#使用requsers和urllib库,爬取与保存及cookic保持会话
import re
import requests
import json
import csv
import urllib.request
s= requests.session()
def url_request(html_s):
    headers={
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language":"zh-CN,zh;q=0.9",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36",
        "Cookie":"Cookie: SNUID=5563F7BFCFCA649FCCAA145CCF1C6D38; IPLOC=CN4201; SUID=9BAC38716B20A00A000000005F0C34E9; SUV=1594635501352002; pgv_pvi=3683231744; sct=1; ld=Mlllllllll2KRRFIlllllVDGStyllllltFA7uZllllylllllRylll5@@@@@@@@@@; pgv_si=s3889248256; LSTMV=288%2C372; LCLKINT=2445"
    }
    #为下载文件设置请求头与cookie
    opener = urllib.request.build_opener()
    opener.addheaders =headers
    urllib.request.install_opener(opener)
    try:
        for i in html_s:
            urllib.request.urlretrieve("https:" + i, filename="./糗事百科video{}".format(i[i.rfind('/'):]))
    except:
        print("下载中断，重新设置请求头")
    finally:
        print("下载完成")
def spider(url):
    header={
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language":"zh-CN,zh;q=0.9",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
    }
    html = s.get(url,headers=header)
    html=html.content.decode("utf-8")
    html_s = re.findall("<source src=\"(.*?)\" type='video/mp4' />",html,re.S)
    urllib.request.HTTPCookieProcessor()
    for i in html_s:
        urllib.request.urlretrieve("https:"+i,filename="./糗事百科video{}".format(i[i.rfind('/'):]))
if __name__ == '__main__':
    num = input("需要多少条视频(页码/页码-页码)：")
    if num.isdigit():
        url = "https://www.qiushibaike.com/video/page/{}/".format(num)
        spider(url)
    else:
        try:
            num_list = num.split("-")
            for i in range(int(num_list[0]),int(num_list[1])):
                url = "https://www.qiushibaike.com/video/page/{}/".format(i)
                spider(url)
        except:
            print("输入错误")
